package cn.bluedot.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.bluedot.model.ScoreVo;

/**
 * @author 作者 zg
 * @version 创建时间：2019年4月12日 下午4:49:40
 */

@Controller
@RequestMapping("/ech")
public class EchartsPhy {

    @RequestMapping("/toolBox")
    @ResponseBody
    public void toolBox(HttpServletRequest request, HttpServletResponse response, Map map) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ScoreVo s1 = new ScoreVo(1, "母猪1", 88, 99, 77, 66, 55, 33, 99);
        ScoreVo s2 = new ScoreVo(2, "母猪2", 55, 79, 87, 86, 35, 83, 29);
        ScoreVo s3 = new ScoreVo(3, "母猪3", 88, 89, 67, 76, 45, 63, 59);
        ScoreVo s4 = new ScoreVo(4, "母猪4", 88, 69, 57, 96, 85, 73, 89);

        List<ScoreVo> list = new ArrayList<ScoreVo>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);

        String head[] = { "第一个星期", "第二个星期", "第三个星期", "第四个星期", "第五个星期", "第六个星期", "第七个星期" };

        // 创建json最外层的Object对象
        JSONObject resultData = new JSONObject();
        resultData.put("text", "生猪养殖曲线表");
        JSONArray arr1 = new JSONArray();
        for (int i = 0; i < head.length; i++) {
            arr1.add(head[i]);
        }
        resultData.put("xAxisData", arr1);

        // 获取list设置的大小
        int size = list.size();
        // 定义数组
        String legend[] = new String[size];

        JSONArray series = new JSONArray();
        for (int i = 0; i < size; i++) {
            JSONObject data = new JSONObject();
            ScoreVo s = list.get(i);
            legend[i] = s.getStudName();
            data.put("name", s.getStudName());
            data.put("type", "line");

            JSONArray arr2 = new JSONArray();
            arr2.add(s.getYuwen());
            arr2.add(s.getShuxue());
            arr2.add(s.getEnglish());
            arr2.add(s.getHistory());
            arr2.add(s.getSport());
            arr2.add(s.getShengwu());
            arr2.add(s.getHuaxue());
            data.put("data", arr2);
            // ------------markLine----------------------

            JSONObject data11 = new JSONObject();
            data11.put("type", "average");
            data11.put("name", "平均值");
            JSONArray arr3 = new JSONArray();
            arr3.add(data11);
            JSONObject data1 = new JSONObject();
            data1.put("data", arr3);
            data.put("markLine", data1);
            // -------------markPoint------------------
            JSONObject data21 = new JSONObject();
            data21.put("type", "max");
            data21.put("name", "最高分");
            JSONObject data22 = new JSONObject();
            data22.put("type", "min");
            data22.put("name", "最低分");
            JSONArray arr31 = new JSONArray();
            arr31.add(data21);
            arr31.add(data22);

            JSONObject data23 = new JSONObject();
            data23.put("data", arr31);

            data.put("markPoint", data23);
            series.add(data);
        }
        resultData.put("series", series);
        JSONArray arr5 = new JSONArray();
        for (int i = 0; i < legend.length; i++) {
            arr5.add(legend[i]);
        }
        resultData.put("legenddata", arr5);
        String json = resultData.toJSONString();
        System.out.println("json=" + json);
        out.println(json);
        out.flush();
        out.close();
    }

}
